#!/usr/bin/env bash

rvm_base_except="selector"

source "$rvm_scripts_path/base"
source "$rvm_scripts_path/functions/list"

__rvm_list_remote()
{
  \typeset version
  rubies=($(
    __list_remote_all |
      __rvm_sed -e 's/.tar.*$//' -e 's/jruby-bin-/jruby-/' |
      __rvm_awk -F/ '{print $NF}' |
      \command \sort -u
  ))

  if
    (( ${#rubies[@]} == 0 ))
  then
    rvm_error "
# No remote rubies available
# Check 'rvm list known' for what can be installed.
"
    return 1
  fi

  printf "%b" "\n# Remote rubies available:\n\n"

  for version in "${rubies[@]}"
  do
    if [[ -r "$rvm_rubies_path/$version/bin/ruby" ]]
    then rvm_log " * $version"
    else rvm_log "   $version"
    fi
  done

  rvm_log "\n<comment># * - installed already</comment>\n"
  rvm_log "<comment># To download and install:</comment> <code>rvm install {name} --binary</code>\n\n"
}

__rvm_list_detect_arch()
{
  if
    [[ -d "$rvm_gems_path/${__version}" && ! -d "$rvm_rubies_path/${__version%%@*}" ]]
  then
    __arch="ruby not installed"
  elif
    [[ ! -x "$rvm_rubies_path/${__version%%@*}/bin/ruby" ]]
  then
    __arch="missing bin/ruby"
  elif
    [[ -f "$rvm_rubies_path/${__version%%@*}/config" ]]
  then
    __arch="$(
      __rvm_awk -F= "\$1==\"target_cpu\" {gsub(/[\"']/,\"\"); print \$2}" < \
        $rvm_rubies_path/${__version%%@*}/config
    )" #'
  else
    __arch="$( __rvm_ruby_config_get "target_cpu" "$rvm_rubies_path/${__version%%@*}/bin/ruby" )"
  fi
  if
    [[ -z "${__arch}" ]]
  then
    case "${__version}" in
      (maglev-*|macruby-*) __arch="x86_64" ;;
      (mruby|mruby-*)      __arch="x86_64" ;;
      (*)                  __arch="broken" ;;
    esac
  fi
}

__rvm_list_show_version_with_arch()
{
  \typeset __arch __prefix __version __suffix
  __prefix="$1"
  __version="$2"
  __suffix="${3:-}"

  __rvm_list_detect_arch

  if rvm_pretty_print stdout
  then __version="${rvm_notify_clr:-}${__version}${rvm_reset_clr:-}"
  fi
  printf "%b" "${__prefix}${__version} [ ${__arch} ]${__suffix}"
}

__rvm_list_gemsets()
{
  \typeset all_rubies version versions ruby_version_name current_ruby selected system_ruby system_version string binary prefix

  if [[ "${1:-""}" == "strings" ]]
  then
    __rvm_list_gemset_strings
    return 0
  fi

  current_ruby="$(__rvm_env_string)"

  all_rubies="$(__rvm_list_strings | sort)"

  printf "%b" "\nrvm gemsets\n"

  versions=($(
    __rvm_cd "${rvm_gems_path:-"$rvm_path/gems"}/"
    __rvm_find . -maxdepth 1 -mindepth 1 -print 2> /dev/null |
      __rvm_awk '/[a-z]*-.*/ {print $NF}' | sort
  ))

  for version in "${versions[@]//.\/}"
  do
    ruby_version_name="$(echo "$version" | __rvm_awk -F"${rvm_gemset_separator:-"@"}" '{print $1}')"

    if [[ "$all_rubies" != *"$ruby_version_name"* ]]
    then continue
    fi
    if [[ "$version" == "$current_ruby" ]]
    then prefix="=> "
    else prefix="   "
    fi
    __rvm_list_show_version_with_arch "\n${prefix}" "$version"
  done

  if [[ -f "$rvm_path/config/default" && -s "$rvm_path/config/default" ]]
  then
    version=$(
      __rvm_grep 'MY_RUBY_HOME' "$rvm_path/config/default" |
        \command \head -1 | __rvm_awk -F"'" '{print $2}' | __rvm_xargs basename --
    )
    if
      [[ -n "$version" ]]
    then
      printf "%b" "\nDefault Ruby (for new shells)\n"
      __rvm_list_show_version_with_arch "\n  " "$version" "\n"
    fi
  fi
  printf "%b" "\n\n"
  return 0
}

__rvm_list_default()
{
  \typeset version string
  if
    [[ "$1" == "string" ]]
  then
    "$rvm_scripts_path/alias" show default 2>/dev/null |
      __rvm_awk -F"${rvm_gemset_separator:-"@"}" '{print $1}' |
      __rvm_xargs basename --
  else
    if
      [[ -L "$rvm_rubies_path/default" ]]
    then
      version=$(
      "$rvm_scripts_path/alias" show default 2>/dev/null |
        __rvm_awk -F"${rvm_gemset_separator:-"@"}" '{print $1}' |
        __rvm_xargs basename --
      )
      if
        [[ -n "$version" ]]
      then
        printf "%b" "\nDefault Ruby (for new shells)\n"
        __rvm_list_show_version_with_arch "\n   " "$version" "\n"
      fi
    fi
  fi
  printf "%b" "\n"
  return 0
}

__rvm_list_ruby_svn_tags()
{
  \typeset -a tags
  \typeset prefix tag

  __rvm_read_lines tags <(
    svn list http://svn.ruby-lang.org/repos/ruby/tags/ | __rvm_awk '/^v1_[8|9]/{print}'
  )

  for tag in "${tags[@]}"
  do
    prefix="$(
      echo ${tag/\//} |
      __rvm_sed 's#^v1_##' |
      __rvm_awk -F'_' '{print "(ruby-)1."$1"."$2}' |
      __rvm_sed 's#p$##'
    )"
    echo "${prefix}-t${tag/\//}"
  done

  return 0
}

__rvm_list_known()
{
  if [[ "${1:-""}" == "strings" ]]
  then
    __rvm_list_known_strings
    return 0
  fi

  if [[ -t 0 ]]
  then
    __rvm_pager_or_cat_v "$rvm_path/config/known"
  else
    \command \cat "$rvm_path/config/known"
  fi

  return $?
}

__rvm_list_rubies()
{
  \typeset current_ruby rubies version selected system_ruby system_version \
    default_ruby string binary prefix

  if [[ "${1:-""}" == "strings" ]]
  then
    __rvm_list_strings
    return 0
  fi

  current_ruby="$( __rvm_env_string )"
  current_ruby="${current_ruby%${rvm_gemset_separator:-"@"}*}"

  default_ruby="$( "$rvm_scripts_path/alias" show default 2>/dev/null )"
  default_ruby="${default_ruby%${rvm_gemset_separator:-"@"}*}"

  rubies=($(
    __rvm_cd "$rvm_rubies_path/"
    __rvm_find . -maxdepth 1 -mindepth 1 -type d 2> /dev/null | sort
  ))

  for version in "${rubies[@]//.\/}"
  do
    if
      [[ "$version" == "$current_ruby" && "$version" == "$default_ruby" ]]
    then
      prefix="=* "
    elif
      [[ "$version" == "$current_ruby" ]]
    then
      prefix="=> "
    elif
      [[ "$version" == "$default_ruby" ]]
    then
      prefix=" * "
    else
      prefix="   "
    fi
    __rvm_list_show_version_with_arch "${prefix}" "$version" "\n"
  done

  if (( ${#rubies[@]} == 0 ))
  then
    printf "%b" "
# No rvm rubies installed yet. Try 'rvm help install'.
"
  else
    if [[ -z "${default_ruby}" ]]
    then
      printf "%b" "
# Default ruby not set. Try 'rvm alias create default <ruby>'.
"
    fi
    printf "%b" "
# => - current
# =* - current && default
#  * - default
"
  fi

  printf "%b" "\n"

  return 0
}

# List all rvm installed rubies, default ruby and system ruby.
# Display the rubies, indicate their architecture and indicate which is currently used.
# This is not meant to be used with scripting. This is for interactive mode usage only.
__rvm_list()
{
  \typeset action="${1:-rubies}"
  shift

  case "${action}" in
    -r|--remote|remote)
       __rvm_list_remote "$@"
       ;;
    known|known_strings|gemsets|default|rubies|strings|ruby_svn_tags)
      __rvm_list_${action} "$@"
      ;;
    help)
      rvm_help list "${action}" "$@"
      ;;
    *)
      rvm_error_help "Unknown action '$action' for 'rvm list'." list "${action}" "$@"
      return 1
      ;;
  esac
}

__rvm_list "$@"
